added Feb 2001 SDK
[windows-sources.git] / shared source / vb / language / shared / guardbase.h
blob83633ce1216ec7a0da64e3a60bf8bb98e1c48c61
1 //-------------------------------------------------------------------------------------------------
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // Misc templates that make exception guarantees.
6 //
7 //-------------------------------------------------------------------------------------------------
9 class GuardBase
11 protected:
12 GuardBase() : m_enabled(true), m_hasRun(false)
16 virtual void DoAction() = 0;
18 public:
20 void RunAction()
22 if ( m_enabled && !m_hasRun )
24 m_hasRun = true;
25 DoAction();
29 bool IsEnabled() const
31 return m_enabled;
34 void SetIsEnabled(bool enabled)
36 m_enabled = enabled;
39 private:
40 // Not copy safe
41 GuardBase(const GuardBase&);
42 GuardBase& operator=(const GuardBase&);
44 bool m_enabled;
45 bool m_hasRun;
48 template <typename T>
49 class ClearPointerGuard : public GuardBase
51 public:
52 ClearPointerGuard(T*& ptr) : m_ptr(ptr)
56 ~ClearPointerGuard()
58 RunAction();
61 protected:
62 virtual __override
63 void DoAction()
65 m_ptr = NULL;
68 private:
69 T*& m_ptr;